package com.mlk.waste.service.impl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Maps;
import com.mlk.core.component.page.Page;
import com.mlk.core.component.page.Pager;
import com.mlk.core.exception.GlobalException;
import com.mlk.core.util.ServerResponse;
import com.mlk.waste.mapper.EngWasteTypeTreeMapper;
import com.mlk.waste.mapper.EngWasteTypeTreePriceMapper;
import com.mlk.waste.po.EngWasteTypeTree;
import com.mlk.waste.po.EngWasteTypeTreePrice;
import com.mlk.waste.service.WasteTypeTreeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WasteTypeTreeServiceImpl implements WasteTypeTreeService {

    @Resource
    private EngWasteTypeTreeMapper engWasteTypeTreeMapper;

    @Resource
    private EngWasteTypeTreePriceMapper engWasteTypeTreePriceMapper;

    /**
     * 添加主分类
     *
     * @param name
     * @return
     */
    @Override
    public ServerResponse addMainType(String name,String unit) {
        EngWasteTypeTree treeNode = new EngWasteTypeTree();
        treeNode.setName(name);
        treeNode.setUnit(unit);
        treeNode.setSeq(String.valueOf(new Date().getTime()));
        int count = this.engWasteTypeTreeMapper.insert(treeNode);
        if(count == 0){
            throw new GlobalException("添加主分类失败");
        }

        return ServerResponse.ok("添加主分类成功");
    }

    /**
     * 查询所有的节点
     * @return
     */
    @Override
    public ServerResponse queryAllNode() {
        return ServerResponse.ok(this.engWasteTypeTreeMapper.selectList(
                Condition.create().orderBy("pid",true).orderBy("seq",true).orderBy("id",true)));
    }

    @Override
    public ServerResponse editTypeName(EngWasteTypeTree tree) {
        int count = this.engWasteTypeTreeMapper.updateById(tree);
        if(count == 0){
            throw new GlobalException("修改名称成功失败");
        }

        return ServerResponse.ok("修改名称成功成功");
    }

    @Override
    public ServerResponse addSon(EngWasteTypeTree tree) {
        tree.setSeq(String.valueOf(new Date().getTime()));
        int count = this.engWasteTypeTreeMapper.insert(tree);
        if(count == 0){
            throw new GlobalException("添加类别失败");
        }

        return ServerResponse.ok("添加类别成功");
    }

    /**
     * 添加节点的浮动价格信息
     * @param price
     * @return
     */
    @Override
    @Transactional
    public ServerResponse addTreeNodePrice(EngWasteTypeTreePrice price) {
        // 判断是否可以添加
        int count = this.engWasteTypeTreePriceMapper.selectCount(new EntityWrapper<>(EngWasteTypeTreePrice.builder().typeId(price.getTypeId()).build()));
        if(count > 0){
            // 说明已经存在一条记录了,现在只能修改了.
            throw new GlobalException("已经存在浮动价格信息了,通过修改进行价格的调整");
        }
        count = this.engWasteTypeTreePriceMapper.insert(price);
        if(count == 0){
            throw new GlobalException("添加浮动价格失败");
        }

        return ServerResponse.ok("添加浮动价格成功");
    }

    /**
     * 删除浮动价格
     * @param id
     * @return
     */
    @Override
    public ServerResponse delTreeNodePrice(Integer id) {
        int count = this.engWasteTypeTreePriceMapper.deleteById(id);
        if(count == 0){
            throw new GlobalException("删除浮动价格失败");
        }

        return ServerResponse.ok("删除浮动价格成功");
    }

    /**
     * 修改浮动价格
     * @param price
     * @return
     */
    @Override
    public ServerResponse editTreeNodePrice(EngWasteTypeTreePrice price) {
       int count = this.engWasteTypeTreePriceMapper.updateById(price);
        if(count == 0){
            throw new GlobalException("修改浮动价格失败");
        }

        return ServerResponse.ok("修改浮动价格成功");
    }

    @Page
    @Override
    public Pager queryAllTreeNodePrice(Integer typeId) {
        return Pager.adapt(this.engWasteTypeTreePriceMapper.selectList(new EntityWrapper<>(EngWasteTypeTreePrice.builder().typeId(typeId).build())));
    }

    @Override
    public ServerResponse openOrClose(EngWasteTypeTree tree) {
        int count = this.engWasteTypeTreeMapper.updateById(tree);
        if(count == 0){
            throw new GlobalException("操作失败");
        }

        return ServerResponse.ok("操作成功");
    }

    /**
     * 获取主分类的列表信息
     * @return
     */
    @Override
    public ServerResponse getMainTypeInfo() {
        // 定义的父级的节点为0的就是最顶层的节点
        return ServerResponse.ok(this.engWasteTypeTreeMapper.selectList(new EntityWrapper<>(EngWasteTypeTree.builder().pid(0).build())));
    }

    /**
     * 获取废品类型的价格信息
     * @param id
     * @return
     */
    @Override
    public ServerResponse getTypePriceInfo(Integer id) {
        List<EngWasteTypeTreePrice> result = this.engWasteTypeTreePriceMapper.selectList(new EntityWrapper<>(EngWasteTypeTreePrice.builder().typeId(id).build()));

        Map<String,Object> map = Maps.newHashMap();

        map.put("typeName",this.engWasteTypeTreeMapper.selectById(id).getName()); // 废品类型的名称
        map.put("typeId",id); // 废品类型的id
        map.put("prices",result.stream().map(EngWasteTypeTreePrice::getPrice).sorted().toArray()); // 所有的价格
        map.put("medIndex",result.size()/2); // 角标的中位数
        return ServerResponse.ok(map);
    }

    /**
     * 获取节点的子节点的信息
     * @param id
     * @return
     */
    @Override
    public ServerResponse getSonTreeNodes(Integer id) {
        return ServerResponse.ok(this.engWasteTypeTreeMapper.selectList(new EntityWrapper<>(EngWasteTypeTree.builder().pid(id).build())));
    }

    /**
     * 修改主分类的计量单位
     * @param tree
     * @return
     */
    @Override
    @Transactional
    public ServerResponse editUnit(EngWasteTypeTree tree) {
        int count = this.engWasteTypeTreeMapper.updateById(tree);
        if(count == 0){
            throw new GlobalException("操作失败");
        }
        return ServerResponse.ok("操作成功");
    }

    /**
     * 删除节点内容
     * @param id
     * @return
     */
    @Override
    @Transactional
    public ServerResponse delTreeNode(Integer id) {
        int count = this.engWasteTypeTreeMapper.selectCount(new EntityWrapper<>(EngWasteTypeTree.builder().pid(id).build()));
        if(count != 0){
            throw new GlobalException("该节点存在子节点,请处理完之后再删除该节点");
        }
        // 删除该节点
        count = this.engWasteTypeTreeMapper.deleteById(id);
        if(count == 0){
            throw new GlobalException("删除类型失败");
        }

        return ServerResponse.ok("删除节点成功");
    }


    /**
     * 获取一个节点的价格信息
     * @param id
     * @return
     */
    @Override
    public ServerResponse getAllPrice(Integer id) {
        return ServerResponse.ok(this.engWasteTypeTreePriceMapper.selectList(new EntityWrapper<>(EngWasteTypeTreePrice.builder().typeId(id).build())));
    }

    /**
     * 修改价格信息
     * @param lists
     * @param typeId
     * @return
     */
    @Override
    @Transactional
    public ServerResponse editTypePrice(List<EngWasteTypeTreePrice> lists, Integer typeId) {
        // 删除之前所有的价钱信息
        this.engWasteTypeTreePriceMapper.delete(new EntityWrapper<>(EngWasteTypeTreePrice.builder().typeId(typeId).build()));
        // 重新添加内容
        if(CollectionUtils.isEmpty(lists)){
            return ServerResponse.ok("添加成功");
        }

        // 否则循环添加就好了
        lists.stream().forEach(element->{
            this.engWasteTypeTreePriceMapper.insert(element);
        });
        //
        return ServerResponse.ok("操作成功");
    }

    /**
     * 交换
     * 本质上就是seq + 1,展示的时候按照这个顺序进行展示就好了.
     * @param id
     * @return
     */
    @Override
    public ServerResponse swap(Integer id,Integer sourceId) {
        return ServerResponse.ok(this.engWasteTypeTreeMapper.seqAdd(id,sourceId));
    }
}
